<?php

namespace app\admin\controller\my;

use app\common\controller\Backend;
use app\common\library\Auth;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Article extends Backend
{

    protected $relationSearch = true;

    /**
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\department\Article;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['createuser'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->hidden(['createuser.password', 'createuser.salt']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    //添加收藏
    public function addcollect($ids = null)
    {
        $row = $this->model->get($ids);
        if (empty($row)) {
            $this->error("作品不存在");
        }
        $ret = $this->createCollect($row->id);
        if ($ret) {
            $row->collect_num += 1;
            $row->save();
        }
        $this->success("收藏成功");
    }

}
